package com.dsr.util;

import com.github.tobato.fastdfs.conn.TrackerConnectionManager;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author dsr
 * @date 2020-06-27
 **/
@Component
public class UploadUtil {

    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private TrackerConnectionManager trackerConnectionManager;
    public String UploadFile(MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        String substring = StringUtils.substring(originalFilename, StringUtils.lastIndexOf(originalFilename, ".")+1);
        StorePath storePath = storageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), substring, null);
        return "http://"+trackerConnectionManager.getTrackerList().get(0).replace("22122", "8088")+"/"+storePath.getFullPath();
    }
}
